<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class EventRes extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $user = $request->get("user");
        return[
            "id"=>$this->id,
            "title"=>$this->title,
            "description"=>$this->whenHas("description"),
            "type"=>$this->type,
            "category"=>$this->category->only(['id','name']),
            "capacity"=>$this->capacity,
            "location"=>$this->location,
            "start_at"=>$this->start_at,
            "end_at"=>$this->end_at,
            "creator"=>UserRes::make($this->creator),
            "tags"=>$this->tags->select(['id','name']),
            "invited_users"=>$this->whenLoaded('invitedUsers'),
            "participants"=>$this->whenLoaded('participants'),
            "participant_count"=>$this->when(!isset($this->description),$this->participants()->count()),
            "comments"=>$this->whenLoaded('comments'),
            "comment_count"=>$this->when(!isset($this->description),$this->comments()->count()),
            "can_join"=> $user ? $user->canJoin($this) : false,
            "can_invite"=> $user ? $user->canInvite($this) : false,
//            "user"=>$user
        ];
    }
}
